<?php

use console\db\channels\TransferTableMigration;
use yii\db\Query;

/**
 * 文章视频转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300010_000004_t_cms_article_video extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * @inheritdoc
     */
    public function validateTransfer(&$data) {
        if (!parent::validateTransfer($data)) {
            return false;
        }
        $exists = (new Query)->from('{{%cms_article}}')->where([
            'id' => $data['article_id']
        ])->exists();
        if ($exists) {
            return true;
        }
        return false;
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'cms_article_video' => 'ts_article_video',
            ],
            'columns' => [
                'article_id',
                'path' => function($data) {
                    if ($data['path']) {
                        if (strpos($data['path'], 'http') !== false) {
                            return $data['path'];
                        }
                        return '/uploads/' . ltrim($data['path'], '/');
                    }
                    return '';
                },
                'le_id' => 'le_video_id',
                'le_unique' => 'video_unique',
            ]
        ];
    }

}
